// pages/visitor_submit_List/index.js
const appInstance = getApp()
import drawQrcode from 'weapp-qrcode'
Component({
  data: {
    activeValues: [0],
    enable: false,
    scrollTop: 0,
    loading_visible:false,
    visible:false,
    page_start:1,
    activeValues: [],
    cur: {},
    position: [
      { value: 'top', text: '顶部弹出' },
      { value: 'left', text: '左侧弹出' },
      { value: 'center', text: '中间弹出' },
      { value: 'bottom', text: '底部弹出' },
      { value: 'right', text: '右侧弹出' },
    ],
  },
  methods: {
    onRefresh() {
      this.setData({ enable: true });
      this.onShow()
      setTimeout(() => {
        this.setData({ enable: false });
      }, 1500);
  },
    onScroll(e) {
      const { scrollTop } = e.detail;
      this.setData({ scrollTop });
    },
    return_visitor_coding_page(){
      wx.switchTab({
        url: "/pages/visitor_coding/index"
      })
    },
    return_visitor_submit_list_page(){
      this.setData({
        todo_content_detail_popup:false
      })
    },
    active_order(e){
      console.log(e)
      let that = this
      let order_id = e.currentTarget.dataset.id
      that.setData({loading_visible:true})
      let header = appInstance.globalData.cookies
      wx.request({
        url: appInstance.globalData.request_url + "factory/active_order",
        method:"PUT",
        header: {
          'Cookie': header,
        },
        data:{
          "order_id":order_id
        },
        success (resp) {
          console.log(resp)
          that.setData({loading_visible:false})
          appInstance.globalData.cookies = resp.cookies[0]
          if(resp.statusCode==200){
            appInstance.globalData.cookies = resp.cookies[0]
            if (!resp.data.result){
              wx.showToast({
                title: '归档失败',
                icon: 'error',
              })
            }else{
              wx.showToast({
                title: resp.data.result.msg,
                icon: 'success',
              })
              that.onShow()
              if(resp.data.result.code==403){
                wx.showToast({
                  title: resp.data,
                  icon: 'error',
                  duration: 2000
                })
              }
            }
          }
        },
        fail(e){
          that.setData({loading_visible:false})
          console.log('服务器请求错误' + e)
        }
      })
    },
    popup_todo_content_detail(e){
      let that = this
      let current_id = e.currentTarget.dataset.id
      that.data.visitor_submit_list.forEach(function(item, index) {
      if (item.id == current_id){
        that.setData({
        todo_content_detail_popup:true,
        todo_content_detail_obj:item
      })}
      });
    },
    last_page:function (e) {
      let page_last_start = this.data.page_start -1
      if (page_last_start <= 0){
        this.setData({page_start:1})
        this.onShow()
      }else{
        this.setData({page_start:page_last_start})
        this.onShow()
      }
    },
    next_page:function (e) {
      let page_next_start = this.data.page_start + 1
      if (page_next_start > this.data.page_total_number){
        this.setData({page_start:1})
        this.onShow()
      }else{
        this.setData({page_start:page_next_start})
        this.onShow()
      }
    },
    jump_user_attachemnt_url_web:function (e) {
      console.log(e)
      let src_url = e.currentTarget.dataset.url
      src_url = src_url.split("?")[0] + "?"
      let access_token = e.currentTarget.dataset.access_token
      src_url = src_url + 'access_token=' + access_token
      wx.navigateTo({
        url: "/pages/form/index",
        success: function(res) {
          // 通过eventChannel向被打开页面传送数据
          console.log('传输的数据是' + src_url)
          res.eventChannel.emit('acceptDataFromOpenerPage', { src_url: src_url })
        }
      })
    },
    handleChange(e) {
      this.setData({
        activeValues: e.detail.value,
      });
    },
    git_user_attachments_link(visitor_id){
      let that = this
      wx.request({
        url: appInstance.globalData.request_url + "factory/git_user_attachments_link",
        // url: appInstance.globalData.request_url + "web?#action=117&cids=1&id=23&menu_id=89&model=factory.visitor&view_type=form",
        method:"GET",
        header: {
          'Cookie': appInstance.globalData.cookies,
          "content-type":null,
        },
        data:{"visitor_id":visitor_id},
        success (resp) {
          if(resp.statusCode==200){
            that.setData({attachment_link_list:resp.data, upload_visible:true})
          }else{
            console.log('获取附件失败--' + resp.statusCode)
          }
        },
        fail (err){
          console.log('获取附件失败' + err)
        }
      })
    },
    display_attachments(e){
      const visitor_id = e.currentTarget.dataset.id
      this.git_user_attachments_link(visitor_id)
    },

    onShow() {
      appInstance.setLoginData(this)
      let that = this
      console.log('submit列表:' + appInstance.globalData.cookies )
      wx.request({
        url: appInstance.globalData.request_url + "factory/visitor_submit_list",
        header: {
          'Cookie': appInstance.globalData.cookies
        },
        data:{
          'page_start':this.data.page_start
        },
        method:"POST",
        dataType:"json",
        success(resp) {
          if (resp.statusCode === 500) {
            console.log(resp)
            // wx.showToast({
            //   title: resp.data,
            //   icon: 'error',
            //   duration: 2000
            // })
          } else if (appInstance.globalData.user_status == 'has_login') {
            console.log('进入了 elseif逻辑   ' + appInstance.globalData)
            console.log(resp)
            that.setData({
              visitor_submit_list:resp.data.result.visitor_submit_list,
              activeValues : resp.data.result.visitor_activeValues,
              attachment_link_list:resp.data.result.attachment_link_list,
              page_total_number:resp.data.result.page_total_number,
            })
          }
        },
        fail(err) {
          console.log(err)
          wx.showToast({
            title: err,
            icon: 'error',
            duration: 2000
          })
        }
      })
    },
    show_qr_code:function (e) {
          let visitor_submit_list = this.data.visitor_submit_list
          visitor_submit_list.forEach((item, index) => {
            if (parseInt(e.currentTarget.dataset.id) == parseInt(item.id)) {
              setTimeout(() => {
                drawQrcode({
                  width: 290,
                  height: 290,
                  canvasId: `qrcodePro`,
                  text:`${e.currentTarget.dataset.id}`,
                  _this: this,
                  image: {
                    imageResource: '/static/images/koyo.png',
                    dx: 110,
                    dy: 100,
                  dWidth: 80,
                  dHeight: 80
                }
                })
              }, 100);
            } 
            this.setData({
              visitor_submit_list:visitor_submit_list
            })
          })
  },

//  二维码弹窗
handlePopup(e) {
  const { item } = e.currentTarget.dataset;
  console.log(e)
  if(this.data.visible){
    this.setData({visible:false})
  }else{
    this.setData({visible:true})
    this.show_qr_code(e)
  }
},
onVisibleChange(e) {
  this.setData({
    upload_visible: e.detail.visible,
    visible:false,
    todo_content_detail_popup:false
  })
  console.log("change")
  drawQrcode({
    width: 0,
    height: 0,
    canvasId: `qrcodePro`,
    text: ``,
    _this: this
  })
},

}
})
